package com.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.Setmeal;
import com.query.QueryPageBean;
import com.redis.RedisConstant;
import com.result.Result;
import com.service.SetmealService;
import com.utils.QiNiuUtils;
import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;



    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try{
            if(imgFile!=null && !imgFile.isEmpty()){
                //1.获取原始图片名称
                String originalFilename = imgFile.getOriginalFilename();
                //2.使用UUID新生成的文件名称
                String newName= UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
                //3.使用七牛云上传图片
                QiNiuUtils.uploadViaByte(imgFile.getBytes(),newName);
                //4.返回数据给页面
                Map<String,String> map=new HashMap<String, String>();
                map.put("domain",QiNiuUtils.DOMAIN);
                map.put("imgName",newName);
                //5.将上传图片名称存入Redis，基于Redis的Set集合存储
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newName);

                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }


    //查询所有带分页+条件
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = setmealService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
        }
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    //查询所有带分页+条件
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Setmeal> all = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    //添加
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal ,Integer [] checkgroupIds) throws CustomException {
            //1.添加套餐基础数据以及套餐与检查组的关联数据
            setmealService.add(setmeal,checkgroupIds);
            if(setmeal.getImg()!=null && setmeal.getImg()!=""){
                //2.添加成功，要记录有用的图片到redis集合中
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
       /* try {
            //1.添加套餐基础数据以及套餐与检查组的关联数据
            setmealService.add(setmeal,checkgroupIds);
            if(setmeal.getImg()!=null && setmeal.getImg()!=""){
            //2.添加成功，要记录有用的图片到redis集合中
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);*/
    }

    //根据id查询
    @RequestMapping("/queryByid")
    public Result queryByid(Integer setmealId){
        try {
            Setmeal setmeal = setmealService.queryById(setmealId);
            Integer[] ids = setmealService.queryByCheckGroup(setmealId);
            Map map=new HashMap();
            map.put("setMeal",setmeal);
            map.put("ids",ids);
            map.put("domain",QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);

    }


    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer [] checkgroupIds) throws CustomException {
            Setmeal setmeal1 = setmealService.queryById(setmeal.getId());
            setmealService.update(setmeal,checkgroupIds);
            if(setmeal.getImg()!=null && setmeal.getImg()!=""){
                //判断数据库以有的图片是否和新传递过来的图片一致
                if(!setmeal1.getImg().equals(setmeal.getImg())){
                    //删除旧的图片
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal1.getImg());
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal1.getImg());
                    //删除七牛云中的旧图片
                    QiNiuUtils.removeFiles(setmeal1.getImg());
                }
                //2.添加成功，要记录有用的图片到redis集合中
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        /*try {
            Setmeal setmeal1 = setmealService.queryById(setmeal.getId());
            setmealService.update(setmeal,checkgroupIds);
            if(setmeal.getImg()!=null && setmeal.getImg()!=""){
                //判断数据库以有的图片是否和新传递过来的图片一致
                if(!setmeal1.getImg().equals(setmeal.getImg())){
                    //删除旧的图片
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal1.getImg());
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal1.getImg());
                    //删除七牛云中的旧图片
                    QiNiuUtils.removeFiles(setmeal1.getImg());
                }
                //2.添加成功，要记录有用的图片到redis集合中
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);*/

    }

    //删除
    @PostMapping("/delete")
    public Result delete(Integer SetmealId) throws CustomException {
        //先查询看看数据库里面有没图片
        Setmeal setmeal = setmealService.queryById(SetmealId);
        if(setmeal.getImg()!=null && setmeal.getImg()!=""){
            //删除七牛云中的图片
            QiNiuUtils.removeFiles(setmeal.getImg());
        }
        setmealService.delete(SetmealId);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }


}
